package com.hotel.action;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hotel.dto.Envelope;
import com.hotel.entity.AdminsEntity;
import com.hotel.service.AdminService;

@Controller
@RequestMapping("/admin")
public class AdminAction {
	private AdminService adminService;
	public AdminAction() {
		this.adminService=new AdminService();
	}
	@RequestMapping("/mainMenu")
	public ModelAndView addAdmin(@RequestParam(value="page", required=false, defaultValue="1")int page){
		ModelAndView model=new ModelAndView("admin/addAdmin");
		Envelope env=adminService.getAdminList(page);
		//System.out.println(env.getObj());
		model.addObject("adminList", env.getObj());
		model.addObject("currentPage",page);
		return model;
	}
	@RequestMapping(value="/addNewAdmin.do",method=RequestMethod.POST)
	@ResponseBody
	public String addNewAdmin(AdminsEntity admin,HttpSession session){
		try{
			return adminService.addNewAdmin(admin).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping("/getAdminInfor")
	@ResponseBody
	public String getAdminInfor(int id){
		try{
			return adminService.getAdmin(id).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/changeAdminInfor.do",method=RequestMethod.POST)
	@ResponseBody
	public String changeAdminInfor(AdminsEntity admin){
		try{
			return adminService.changeAdmin(admin).toString();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/deleteAdmin.do",method=RequestMethod.POST)
	@ResponseBody
	public String deleteAdmin(int id){
		try{
			return adminService.deleteAdmin(id).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
}
